﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using SpiderMaster.AspNet.Management;
using SpiderMaster.AspNet.TradingSystem.BusinessLogicLayer;
using SpiderMaster.Net;

namespace SpiderMaster.AspNet
{
    /// <summary> Trading Center.
    /// </summary>
    [ReganSchroder(19, 10, 2009)]
    public sealed partial class TradingCenter : OperatingSystem<TradingSystemData>, IStartable<TradingCenter>
    {
        private TradingCenter()
            : base()
        { try { LoadData(); } catch /* Ignor */ { } }

        private static TradingCenter _Instance;

        /// <summary> Gets the Instance.
        /// </summary>
        /// <value>The Instance.</value>
        public static TradingCenter Current { get { return GetInstance(); } }

        /// <summary> List of all Log-Entries in the Logfile
        /// </summary>
        public List<TradingSystemData.ManagerEntry> Entries { get { return Persisted.ManagerEntries; } }

        /// <summary> Gets a value indicating whether membership DB can connect.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if membership DB can connect; otherwise, <c>false</c>.
        /// </value>
        public bool MembershipDBCanConnect
        {
            get
            {
                try
                {
                    Membership.GetAllUsers();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary> Gets the SQL connection string.
        /// </summary>
        /// <value>The SQL connection string.</value>
        public string SQLConnectionString
        {
            get
            {
                try // TODO: Context not available in application start.
                {
                    if (HttpContext.Current.IsNotNull())
                        if (HttpContext.Current.Request.IsNotNull())
                            if (HttpContext.Current.Request.IsLocal)
                                return Persisted.LocalSQLConnectionString;
                }
                catch { }

                if (Persisted.HostedSQLConnectionString.IsNotNullOrEmpty())
                    return Persisted.HostedSQLConnectionString; // If local cant pass..
                else return Persisted.LocalSQLConnectionString;
            }
        }

        /// <summary> Gets a value indicating whether [reload plugins on initialize].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [reload plugins on initialize]; otherwise, <c>false</c>.
        /// </value>
        public bool ReloadPluginsOnInitialize { get { return Persisted.ReloadPluginsOnInitialize; } }

        /// <summary> Gets the site URL.
        /// </summary>
        /// <value>The site URL.</value>
        public string SiteUrl { get { return GetSiteUrl(); } }

        /// <summary> Gets or sets the last notification sent.
        /// </summary>
        /// <value>The last notification sent.</value>
        public DateTime LastNotificationSent
        {
            get { return Persisted.LastNotificationSent; }
            internal set
            {
                Persisted.LastNotificationSent = value;
                SaveData();
            }
        }

        /// <summary> Gets a value indicating whether SQLDB can connect.
        /// </summary>
        /// <value><c>true</c> if [SQLDB can connect]; otherwise, <c>false</c>.</value>
        public bool SQLDBCanConnect
        {
            get
            {
                try
                {
                    Stats.GetStatistics();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary> Gets a value indicating whether SQLDB can not connect.
        /// </summary>
        /// <value><c>true</c> if SQLDB can not connect; otherwise, <c>false</c>.</value>
        public bool SQLDBCanNotConnect { get { return !SQLDBCanConnect; } }
    }
}
